<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Cart extends Model
{
    protected $guarded=[];
    protected $casts=[
       'attributeArr'=>'array',
    ];
    //根据商品id获取商品数据
    public function getGood($goods_id){
    
  return $good=Goods::where('id',$goods_id)->first();
    }
    //购物车关联商品
    public function getGoods(){
        return $this->belongsTo(Goods::class,'goods_id','id');
    }
    //购物车关联货品
    public function getProducts(){
        return $this->belongsTo(Product::class,'product_id','id');
    }
    //根据货品id获取货品的属性组合
    public function attribute($attr){
     return   json_encode($attributes=Attribute::whereIn('id',$attr)->pluck('name')->toArray(),JSON_UNESCAPED_UNICODE);
    }
    public function checkShoucang(){
        return $this->hasOne(Collect::class,'goods_id','goods_id');
    }
}
